package cn.b.sky.form.controller;

import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.enums.DwzCallbackType;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.form.constans.CustomerPageConstans;
import cn.b.sky.form.enums.QueryDataType;
import cn.b.sky.form.enums.QueryOperatorEnum;
import cn.b.sky.form.enums.TimeUnit;
import cn.b.sky.form.model.*;
import cn.b.sky.form.service.BusinessFieldService;
import cn.b.sky.form.service.BusinessTableService;
import cn.b.sky.form.service.BusinessTypeService;
import cn.b.sky.form.service.CustomQueryService;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.service.UserService;
import cn.b.sky.user.service.model.UserInfo;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/customQuery/*")
public class CustomQueryController extends SecurityController {
  
  private static final int PAGESIZE=20;
  @Resource
  UserService userService;

  @Resource
  BusinessFieldService businessFieldService;

  @Resource
  CustomQueryService customQueryService;

  @Resource
  BusinessTypeService businessTypeService;

  @Resource
  BusinessTableService businessTableService;

  /**
   * 
   * <strong>Title : customQuery<br></strong>
   * <strong>Description : </strong>自定义查询主页面<br> 
   * <strong>Create on : 2016年12月12日 下午1:57:10<br></strong>
   * <p>
   * @param model
   * @param navTabId
   * @param selectType
   * @param selectNode
   * @return
   * String
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("customQuery")
  public String customQuery(Model model, String navTabId, String selectType, String selectNode) {
    List<BusinessType> types = customQueryService.getQueryTree(getSiteCode(), SecurityContextUtil.getCurrentUserName());
    model.addAttribute("types", types);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("selectType", selectType);
    model.addAttribute("selectNode", selectNode);
    return "workflow/customQuery/queryScreen";
  }

  /**
   * 
   * <strong>Title : newCustomQuery<br></strong>
   * <strong>Description : </strong>新建查询<br> 
   * <strong>Create on : 2016年12月12日 下午1:56:59<br></strong>
   * <p>
   * @param model
   * @param navTabId
   * @param type
   * @return
   * String
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("newCustomQuery")
  public String newCustomQuery(Model model, String navTabId, String type) {
    if (businessTableService.getTableIsExist(getSiteCode(), type) == 0) {
      throw ImeException.newException("businessTableNotExist.M");
    }
    BusinessType businessType = businessTypeService.getBusinessTypeByPrimaryKey(getSiteCode(), type);
    QueryScreen queryScreen = new QueryScreen();
    queryScreen.setScreenName("新建查询[" + businessType.getBusinessTypeText() + "]");
    model.addAttribute("queryScreen", queryScreen);
    ScreenConfig screenConfig = new ScreenConfig();
    List<QueryFieldCondition> queryConditions = new ArrayList<QueryFieldCondition>();
    queryConditions.add(new QueryFieldCondition(CustomerPageConstans.BUSINESSCODE, CustomerPageConstans.BUSINESSCODE, "业务编号", QueryDataType.STRING.getValue(),
        QueryOperatorEnum.EQ.getValue(), null, null, null, null, null));
    queryConditions.add(new QueryFieldCondition(CustomerPageConstans.CREATEUSER, CustomerPageConstans.CREATEUSER, "创建人",  QueryDataType.STRING.getValue(),
        QueryOperatorEnum.IN.getValue(), null, null, null, null, null));
    queryConditions.add(new QueryFieldCondition(CustomerPageConstans.CREATEDATETIME, CustomerPageConstans.CREATEDATETIME, "创建时间", QueryDataType.DATETIME.getValue(),
        QueryOperatorEnum.SC.getValue(), null, null, null, null, TimeUnit.DAY.getValue()));
    screenConfig.setQueryConditions(queryConditions);
    List<String> selectedFields = new ArrayList<String>();
    selectedFields.add(CustomerPageConstans.BUSINESSCODE);
    selectedFields.add(CustomerPageConstans.CREATEUSER);
    selectedFields.add(CustomerPageConstans.CREATEDATETIME);
    screenConfig.setSelectedFields(selectedFields);
    model.addAttribute("screenConfig", screenConfig);
    List<BusinessField> allConditions = businessFieldService.selectBusinessFields(getSiteCode(), type);
    Map<String, String> qConditions = new HashMap<String, String>();

    for (QueryFieldCondition qc : screenConfig.getQueryConditions()) {
      qConditions.put(qc.getBusinessField(), qc.getBusinessField());
    }
    if (screenConfig.getMoreQueryConditions() != null) {
      for (QueryFieldCondition qc : screenConfig.getMoreQueryConditions()) {
        qConditions.put(qc.getBusinessField(), qc.getBusinessField());
      }
    }
    Map<String, String> selectedFieldMap = new HashMap<String, String>();
    for (String field : screenConfig.getSelectedFields()) {
      selectedFieldMap.put(field, field);
    }
    List<BusinessField> conditions = new ArrayList<BusinessField>();
    for (BusinessField businessField : allConditions) {
      if (!qConditions.containsKey(businessField.getBusinessField())) {
        conditions.add(businessField);
      }
      if (selectedFieldMap.containsKey(businessField.getDbField())) {
        businessField.setCheckedFlag(1);
      } else {
        businessField.setCheckedFlag(0);
      }
    }

    model.addAttribute("conditions", conditions);
    model.addAttribute("allFields", allConditions);
    model.addAttribute("businessType", type);

    Map<String, Object> result = query(type, screenConfig, 1, PAGESIZE);
    List<Map<String, Object>> data =(List<Map<String, Object>>) result.get("data");
    int totalCount = (int) result.get("count");
    model.addAttribute("totalCount", totalCount);
    model.addAttribute("data", data);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("pattern", "add");
    return "workflow/customQuery/customQuery";
  }

  /**
   * 
   * <strong>Title : query<br></strong>
   * <strong>Description : </strong>分页查询<br> 
   * <strong>Create on : 2016年12月7日 下午2:44:21<br></strong>
   * <p>
   * @param businessType
   * @param screenConfig
   * @param startRow
   * @param pageSize
   * @return
   * List<Map<String,Object>>
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  Map<String, Object> query(String businessType, ScreenConfig screenConfig, int startRow, int pageSize) {
    List<QueryFieldCondition> whereConditions = new ArrayList<QueryFieldCondition>();

    for (QueryFieldCondition qc : screenConfig.getQueryConditions()) {
      whereConditions.add(qc);
    }
    if (screenConfig.getMoreQueryConditions() != null) {
      for (QueryFieldCondition qc : screenConfig.getMoreQueryConditions()) {
        whereConditions.add(qc);
      }
    }
    BusinessType type = businessTypeService.getBusinessTypeByPrimaryKey(getSiteCode(), businessType);
   
    Map<String, Object> result= customQueryService.queryRows(type.getDbTable(), "*", whereConditions,
        screenConfig.getOrderBy(), startRow, pageSize);
    List<Map<String, Object>> data =(List<Map<String, Object>>) result.get("data");
    List<BusinessField> allConditions = businessFieldService.selectBusinessFields(getSiteCode(), businessType);
    List<BusinessField> enumList = new ArrayList<BusinessField>();
    //查找枚举字段
    for (BusinessField field : allConditions) {
      if (QueryDataType.STATICOPTION.getValue().equals(field.getDataType())) {
        enumList.add(field);
      }
    }
    //为枚举字段赋值
    if (enumList.size() > 0) {
      Map<String, Map<String, String>> enumValues = new HashMap<String, Map<String, String>>();
      for (Map<String, Object> row : data) {
        for (BusinessField field : enumList) {
          String value = (String) row.get(field.getDbField());
          if (StringUtils.isBlank(value)) {
            continue;
          }
          if (enumValues.containsKey(field.getDbField()) && enumValues.get(field.getDbField()).containsKey(value)) {
            //缓存中已存在则直接从缓存中读取
            row.put(field.getDbField(), enumValues.get(field.getDbField()).get(value));
          } else {
            //不存在则从数据库中读取
            BusinessFieldValue businessFieldValue = businessFieldService
                .getBusinessFieldValueByPrimaryKey(getSiteCode(), businessType, field.getBusinessField(), value);
            if (businessFieldValue != null) {
              row.put(field.getDbField(), businessFieldValue.getValueText());
            }
            if (enumValues.containsKey(field.getDbField())) {
              //缓存中如果已经存在这个字段的枚举列表则添加新的枚举值
              enumValues.get(field.getDbField()).put(value, row.get(field.getDbField()).toString());
            } else {
              //缓存中不存在这个字段，则添加这个字段的缓存
              Map<String, String> map = new HashMap<String, String>();
              map.put(value, row.get(field.getDbField()).toString());
              enumValues.put(field.getDbField(), map);
            }
          }

        }
      }
    }
    return result;
  }

  /**
   * 
   * <strong>Title : getRowsCount<br></strong>
   * <strong>Description : </strong>获得总条数<br> 
   * <strong>Create on : 2016年12月7日 下午2:44:31<br></strong>
   * <p>
   * @param businessType
   * @param screenConfig
   * @return
   * int
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  int getRowsCount(String businessType, ScreenConfig screenConfig) {
    List<QueryFieldCondition> whereConditions = new ArrayList<QueryFieldCondition>();
    for (QueryFieldCondition qc : screenConfig.getQueryConditions()) {
      whereConditions.add(qc);
    }
    if (screenConfig.getMoreQueryConditions() != null) {
      for (QueryFieldCondition qc : screenConfig.getMoreQueryConditions()) {
        whereConditions.add(qc);
      }
    }
    BusinessType type = businessTypeService.getBusinessTypeByPrimaryKey(getSiteCode(), businessType);
    int count = customQueryService.getRowsCount(type.getDbTable(), whereConditions);
    return count;
  }

  /**
   * 
   * <strong>Title : getUserAndEnum<br></strong>
   * <strong>Description : </strong>获得用户描述和枚举描述<br> 
   * <strong>Create on : 2016年12月12日 下午2:00:32<br></strong>
   * <p>
   * @param queryConditions
   * @param businessType
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  void getUserAndEnum(List<QueryFieldCondition> queryConditions, String businessType) {
    if (queryConditions != null) {
      for (QueryFieldCondition qc : queryConditions) {
        if (("CREATEUSER".equals(qc.getFieldName()) || "MODIFYUSER".equals(qc.getFieldName()))
            && qc.getFieldValues() != null) {
          String[] names = new String[qc.getFieldValues().length];
          for (int i = 0; i < qc.getFieldValues().length; i++) {
            UserInfo user = userService.selectUser(getSiteCode(), qc.getFieldValues()[i]);
            if (user != null) {
              names[i] = user.getUserText();
            } else {
              names[i] = "";
            }
          }
          qc.setUserNames(names);
        } else if ("STATICOPTION".equals(qc.getDataType())) {
          BusinessField field = businessFieldService.getBusinessFieldAndValues(getSiteCode(), businessType,
              qc.getBusinessField());
          if (field != null) {
            qc.setBusinessFieldValueList(field.getBusinessFieldValueList());
          }
        }
      }
    }

  }

  /**
   * 
   * <strong>Title : getEnumList<br></strong>
   * <strong>Description : </strong>获得静态枚举列表<br> 
   * <strong>Create on : 2016年12月12日 下午2:01:15<br></strong>
   * <p>
   * @param model
   * @param navTabId
   * @param businessType
   * @param businessField
   * @return
   * List<BusinessFieldValue>
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @ResponseBody
  @RequestMapping("getEnumList")
  public List<BusinessFieldValue> getEnumList(Model model, String navTabId, String businessType, String businessField) {
    BusinessField field = businessFieldService.getBusinessFieldAndValues(getSiteCode(), businessType, businessField);
    if (field != null) {
      return field.getBusinessFieldValueList();
    } else {
      return null;
    }
  }

  /**
   * 
   * <strong>Title : queryData<br></strong>
   * <strong>Description : </strong>查询数据<br> 
   * <strong>Create on : 2016年12月12日 下午2:01:32<br></strong>
   * <p>
   * @param model
   * @param navTabId
   * @param businessType
   * @param queryConditions
   * @param moreQueryConditions
   * @param startRow
   * @param pageSize
   * @return
   * Map<String,Object>
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @ResponseBody
  @RequestMapping("queryData")
  public Map<String, Object> queryData(Model model, String navTabId, String businessType, String queryConditions,
                                       String moreQueryConditions, int startRow, int pageSize) {
    ScreenConfig screenConfig = new ScreenConfig();
    if (!StringUtils.isBlank(queryConditions)) {
      screenConfig.setQueryConditions(JSONArray.parseArray(queryConditions, QueryFieldCondition.class));
    }
    if (!StringUtils.isBlank(moreQueryConditions)) {
      screenConfig.setMoreQueryConditions(JSONArray.parseArray(moreQueryConditions, QueryFieldCondition.class));
    }
    Map<String, Object> result = query(businessType, screenConfig, startRow, pageSize);
    return result;

  }

  /**
   * 
   * <strong>Title : saveQuery<br></strong>
   * <strong>Description : </strong>保存自定义查询<br> 
   * <strong>Create on : 2016年12月12日 下午2:05:58<br></strong>
   * <p>
   * @param model
   * @param navTabId
   * @param businessType
   * @param queryConditions
   * @param moreQueryConditions
   * @param selectedFields
   * @param screenName
   * @param shareFlag
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @ResponseBody
  @RequestMapping("saveQuery")
  public DWZResult saveQuery(Model model, String navTabId, String businessType, String queryConditions,
                             String moreQueryConditions, String selectedFields, String screenName, int shareFlag) {
    ScreenConfig screenConfig = new ScreenConfig();
    if (!StringUtils.isBlank(queryConditions)) {
      screenConfig.setQueryConditions(JSONArray.parseArray(queryConditions, QueryFieldCondition.class));
    }
    if (!StringUtils.isBlank(moreQueryConditions)) {
      screenConfig.setMoreQueryConditions(JSONArray.parseArray(moreQueryConditions, QueryFieldCondition.class));
    }
    if (!StringUtils.isBlank(selectedFields)) {
      screenConfig.setSelectedFields(JSONArray.parseArray(selectedFields, String.class));
    }
    QueryScreen queryScreen = new QueryScreen();
    queryScreen.setSiteCode(getSiteCode());
    queryScreen.setBusinessType(businessType);
    queryScreen.setScreenName(screenName);
    queryScreen.setShareFlag(shareFlag);
    queryScreen.setScreenConfig(JSONArray.toJSONString(screenConfig));
    setCreateInfo(queryScreen);
    customQueryService.updateQueryScreen(queryScreen);
    return dialogAjaxDone(navTabId, null, null, null);
  }

  /**
   * 
   * <strong>Title : addQuery<br></strong>
   * <strong>Description : </strong>添加新的查询<br> 
   * <strong>Create on : 2016年12月12日 下午2:06:52<br></strong>
   * <p>
   * @param model
   * @param request
   * @param navTabId
   * @param businessType
   * @param queryConditions
   * @param moreQueryConditions
   * @param selectedFields
   * @param screenName
   * @param shareFlag
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @ResponseBody
  @RequestMapping("addQuery")
  public DWZResult addQuery(Model model, HttpServletRequest request, String navTabId, String businessType,
                            String queryConditions, String moreQueryConditions, String selectedFields, String screenName, int shareFlag) {
    QueryScreen qs = customQueryService.selectQueryScreen(getSiteCode(), businessType, screenName,
        SecurityContextUtil.getCurrentUserName());
    if (qs != null) {
      return ajaxError(getImeViewMessageI18n(null, "name.C,AlreadyExists.C"));
    }
    ScreenConfig screenConfig = new ScreenConfig();
    if (!StringUtils.isBlank(queryConditions)) {
      screenConfig.setQueryConditions(JSONArray.parseArray(queryConditions, QueryFieldCondition.class));
    }
    if (!StringUtils.isBlank(moreQueryConditions)) {
      screenConfig.setMoreQueryConditions(JSONArray.parseArray(moreQueryConditions, QueryFieldCondition.class));
    }
    if (!StringUtils.isBlank(selectedFields)) {
      screenConfig.setSelectedFields(JSONArray.parseArray(selectedFields, String.class));
    }
    QueryScreen queryScreen = new QueryScreen();
    queryScreen.setSiteCode(getSiteCode());
    queryScreen.setBusinessType(businessType);
    queryScreen.setScreenName(screenName);
    queryScreen.setShareFlag(shareFlag);
    queryScreen.setScreenConfig(JSONArray.toJSONString(screenConfig));
    setCreateInfo(queryScreen);
    customQueryService.insertQueryScreen(queryScreen);
    String forwardUrl = getForwardUrl(request) + "/customQuery/customQuery?navTabId=" + navTabId + "&selectType="
        + businessType + "&selectNode=" + screenName;
    return navTabAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, DwzCallbackType.FORWARD,
        forwardUrl, null, null);
  }

  /**
   * 
   * <strong>Title : editCustomQuery<br></strong>
   * <strong>Description : </strong>编辑自定义查询<br> 
   * <strong>Create on : 2016年12月12日 下午2:07:35<br></strong>
   * <p>
   * @param model
   * @param navTabId
   * @param type
   * @param screenName
   * @param createUser
   * @return
   * String
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("editCustomQuery")
  public String editCustomQuery(Model model, String navTabId, String type, String screenName, String createUser) {
    QueryScreen queryScreen = customQueryService.selectQueryScreen(getSiteCode(), type, screenName, createUser);
    ScreenConfig screenConfig = JSONArray.parseObject(queryScreen.getScreenConfig(), ScreenConfig.class);

    model.addAttribute("screenConfig", screenConfig);
    model.addAttribute("queryScreen", queryScreen);
    List<BusinessField> allConditions = businessFieldService.selectBusinessFields(getSiteCode(), type);
    Map<String, String> qConditions = new HashMap<String, String>();

    for (QueryFieldCondition qc : screenConfig.getQueryConditions()) {
      qConditions.put(qc.getBusinessField(), qc.getBusinessField());
    }
    if (screenConfig.getMoreQueryConditions() != null) {
      for (QueryFieldCondition qc : screenConfig.getMoreQueryConditions()) {
        qConditions.put(qc.getBusinessField(), qc.getBusinessField());
      }
    }
    Map<String, String> selectedFieldMap = new HashMap<String, String>();
    for (String field : screenConfig.getSelectedFields()) {
      selectedFieldMap.put(field, field);
    }
    List<BusinessField> conditions = new ArrayList<BusinessField>();
    for (BusinessField businessField : allConditions) {
      if (!qConditions.containsKey(businessField.getBusinessField())) {
        conditions.add(businessField);
      }
      if (selectedFieldMap.containsKey(businessField.getDbField())) {
        businessField.setCheckedFlag(1);
      } else {
        businessField.setCheckedFlag(0);
      }
    }
    getUserAndEnum(screenConfig.getQueryConditions(), type);
    getUserAndEnum(screenConfig.getMoreQueryConditions(), type);
    model.addAttribute("conditions", conditions);
    model.addAttribute("allFields", allConditions);
    model.addAttribute("businessType", type);

    Map<String, Object> result = query(type, screenConfig, 1, PAGESIZE);
    List<Map<String, Object>> data =(List<Map<String, Object>>) result.get("data");
    int totalCount = (int) result.get("count");
    model.addAttribute("totalCount", totalCount);
    model.addAttribute("data", data);
    model.addAttribute("navTabId", navTabId);
    if (SecurityContextUtil.getCurrentUserName().equals(createUser)) {
      model.addAttribute("pattern", "edit");
    } else {
      model.addAttribute("pattern", "view");
    }

    return "workflow/customQuery/customQuery";
  }

  /**
   * 
   * <strong>Title : deleteQuery<br></strong>
   * <strong>Description : </strong>删除自定义查询<br> 
   * <strong>Create on : 2016年12月12日 下午2:08:00<br></strong>
   * <p>
   * @param model
   * @param request
   * @param navTabId
   * @param businessType
   * @param screenName
   * @param createUser
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @ResponseBody
  @RequestMapping("deleteQuery")
  public DWZResult deleteQuery(Model model, HttpServletRequest request, String navTabId, String businessType,
                               String screenName, String createUser) {
    customQueryService.deleteQueryScreen(getSiteCode(), businessType, screenName, createUser);
    String forwardUrl = getForwardUrl(request) + "/customQuery/customQuery?navTabId=" + navTabId;
    return navTabAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, DwzCallbackType.FORWARD,
        forwardUrl, null, null);
  }

  /**
   * 
   * <strong>Title : renameQuery<br></strong>
   * <strong>Description : </strong>重命名<br> 
   * <strong>Create on : 2016年12月12日 下午2:08:25<br></strong>
   * <p>
   * @param model
   * @param request
   * @param navTabId
   * @param businessType
   * @param screenName
   * @param createUser
   * @param newName
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @ResponseBody
  @RequestMapping("renameQuery")
  public DWZResult renameQuery(Model model, HttpServletRequest request, String navTabId, String businessType,
                               String screenName, String createUser, String newName) {
    QueryScreen qs = customQueryService.selectQueryScreen(getSiteCode(), businessType, newName,
        SecurityContextUtil.getCurrentUserName());
    if (qs != null) {
      return ajaxError(getImeViewMessageI18n(null, "name.C,AlreadyExists.C"));
    }
    QueryScreen queryScreen = new QueryScreen();
    queryScreen.setSiteCode(getSiteCode());
    queryScreen.setCreateUser(createUser);
    queryScreen.setBusinessType(businessType);
    queryScreen.setScreenName(screenName);
    queryScreen.setNewName(newName);
    setModifyInfo(queryScreen);
    customQueryService.updateQueryScreenName(queryScreen);
    String forwardUrl = getForwardUrl(request) + "/customQuery/customQuery?navTabId=" + navTabId + "&selectType="
        + businessType + "&selectNode=" + newName;
    return navTabAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, DwzCallbackType.FORWARD,
        forwardUrl, null, null);

  }

}
